﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetaNote.Model;
using DocumentDictionary = System.Collections.Generic.Dictionary<System.Guid, MetaNote.Model.Document>;

namespace MetaNote
{
    public partial class SaveWhichDocumentsForm : Form
    {
        private DocumentDictionary m_documents = new DocumentDictionary();

        public SaveWhichDocumentsForm(DocumentDictionary documents)
        {
            InitializeComponent();
            m_documents = documents;
        }

        public void SaveWhichDocumentsForm_Load(object sender, EventArgs e)
        {
            foreach (var document in m_documents.Values)
            {
                checkedListBox1.Items.Add(document, true);                
            }

            bool showHelpers = (m_documents.Values != null && m_documents.Values.Count > 1);

            linkAll.Visible = showHelpers;
            linkNone.Visible = showHelpers;
            linkToggle.Visible = showHelpers;
            linksLabel.Visible = showHelpers;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (var item in checkedListBox1.CheckedItems)
            {
                var document = item as Document;
                if (!document.Save(this))
                {
                    this.DialogResult = DialogResult.Cancel;
                }
            }
            //TODO: Save selected documents!
            //if it fails... then tell the end user, 
            //and cancel out of this dialog so they 
            //can use the main form to deal with the issues.

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            checkall(true);
        }

        private void linkNone_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            checkall(false);
        }

        private void linkToggle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                bool isChecked = checkedListBox1.GetItemChecked(i);
                checkedListBox1.SetItemChecked(i, !isChecked);                
            }
        }

        private void checkall(bool value)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, value);                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
